define(function (require, exports, module) {
    var PDFTextObject = require('../../PDFData/Text/Reader_TextObject.js');
    var PDFTextPage = require('../../PDFData/Text/Reader_TextPage.js');
    var CReader_TextPageSelect = require('./Reader_TextPageSelect.js');
    var PDFPoint = WebPDF.PDFPoint;
    var PDFRect = WebPDF.PDFRect;
    var RectUtils = WebPDF.RectUtils;
    var CommonTools = WebPDF.Common;
    var CaretInfo = WebPDF.PDFData.Text.CaretInfo;
    var TextBoundInfo = WebPDF.PDFData.Text.TextBoundInfo;

    var SELECT_STATE = {
        SELECT_MODE_NORMAL: 0,
        SELECT_MODE_IN_SELECTED: 1,
        SELECT_MODE_SELECTING: 2,
        SELECT_MODE_BOUND: 3
    };
    WebPDF.TextSelect.SELECT_STATE = SELECT_STATE;

    var CURSOR_STATE = {
        CURSOR_NORMAL: 0,
        CURSOR_SELECT: 1,
        CURSOR_ARROW: 2,
        CURSOR_BOUND: 3,
        CURSOR_QKSCH: 4
    };
    WebPDF.TextSelect.CURSOR_STATE = CURSOR_STATE;

    /**
     * @description 文本页面类
     * @class 文本页面类
     * @ignore
     * @constructor
     */
    WebPDF.TextSelect.TextPage = function () {
        /**
         * @description PDF文本页面对象
         * @public
         * @field
         * @type {WebPDF.PDFData.Text.PDFTextPage}
         */
        this.textPage = null;
        /**
         * @description 文本页面的选择文本信息对象
         * @public
         * @field
         * @type {WebPDF.TextSelect.CReader_TextPageSelect}
         */
        this.pageSelectInfo = new CReader_TextPageSelect();
    };
    var TextPage = WebPDF.TextSelect.TextPage;

    /**
     * @description 页面文本选择区域类
     * @class 页面文本选择区域类
     * @ignore
     * @constructor
     */
    WebPDF.TextSelect.TextSelectedInfo = function () {
        /**
         * @description 页面索引(从0开始)
         * @public
         * @field
         * @type {Number}
         */
        this.pageIndex = -1;
        /**
         * @description 页面文本选择区域数组
         * @public
         * @field
         * @type {Array.<WebPDF.PDFRect>}
         */
        this.selectedRectArray = [];
    };

    /**
     * @description Text selection tool
     * @class Text selection tool class
     * @ignore
     * @constructor
     * @param {WebPDF.ReaderApp} readerApp App instance
     */
    var CReader_TextSelectTool = function (readerApp) {
        var _readerApp = readerApp,
            _curPageView = null,
            _textPageList = [],
            /**
             * 被选中的页面索引列表
             * @type {Array.<Number>}
             * @private
             */
            _selectedPageArray = [],
            _isBoundSelect = false,
            _startWindowPoint = new PDFPoint(0, 0),
            _endWindowPoint = new PDFPoint(0, 0),
            _startPageIndex = -1,
            _selectMode = SELECT_STATE.SELECT_MODE_NORMAL,
            _cursorState = CURSOR_STATE.CURSOR_SELECT, //鼠标样式
            _isLButtonDown = false,
            _highlightDivCreatedPageList = {},  // 存储已经创建highlight div的页面索引,格式为 :   页面索引：true/false
            _jqBoundSelectionRect = null, //标记范围选择区域Div元素jQuery对象
            _jqSelectionOperationBarDiv = null, //选完文本后操作Div元素jQuery对象
            _curCursorStyleName = null,
            _copyFrame = null,
            _isCopyFrameLoaded = false,
            _copyBtnID = null,
            _self = this;
        var _curCaretInfo = new CaretInfo();
        var _startCaretInfo = new CaretInfo();
        var _beginSelectionPoint = new PDFPoint(0, 0),
            _endSelectionPoint = new PDFPoint(0, 0);

        
        function _initCopyFrame(){
            //try{
                if(!_copyFrame){
                    _copyFrame = document.createElement("iFrame");
                    _copyFrame.id = _readerApp.getAppId() + "-copy-frame";
                    _copyFrame.className = "fwr-copy-frame";
                    $('body').append(_copyFrame);
                    $("#" + _copyFrame.id).on("load",function(){
                        _copyFrame.contentWindow.title = "Reading View.";
                        _isCopyFrameLoaded = true;
                    });
                }
           /// }catch(ex){
           //     console.error(ex);
           // }
        }
        _initCopyFrame();

        function _copyTextToFrame(){
            if (!_copyFrame.contentWindow) {
                console.warn( "Cannot focus the copy frame's window, because it's null. Copy frame load status = "
                    +  _isCopyFrameLoaded + ".");
                return;
            }
            $(_copyFrame.contentWindow.document.body).empty().append("<p>" + _self.getSelectedText() + "</p>");
            //try {
                _copyFrame.contentWindow.focus();
                if (typeof window.getSelection != "undefined") {
                	var sel = _copyFrame.contentWindow.getSelection();
                    sel.removeAllRanges();
                    sel.selectAllChildren(_copyFrame.contentWindow.document.body);
                } else if (typeof document.execCommand != "undefined") {
                    _copyFrame.contentWindow.document.execCommand("SelectAll");
                }
            //} catch(ex) {
            //    console.error(ex);
           // }
        }
        
        /**
         * @description 获取标记范围选择区域的div元素jQuery对象, 如果不存在，则创建
         * @private
         * @return {Object} 标记范围选择区域元素jQuery对象;
         */
        function _getBoundSelectionRectJQueryObj() {
            if (!_jqBoundSelectionRect) {
                var docView = _readerApp.getMainView().getDocView();
                var firstPageView = docView.getPageView(0);
                var jqPagesContainerDiv = $("#" + docView.getDocViewContainerID());
                var selectionDivID = docView.getDocViewContainerID() + "_text_bound_select";
                var divHtml = "<div class='fwr-text-bound-selection fwr-hidden' id='" + selectionDivID + "'></div>";
                if (firstPageView != null && firstPageView.isContentCreated()) {
                    //通过parent去添加，不利于维护，并且效率也不高，后续考虑调整
                    $("#" + firstPageView.getPageViewContainerID()).parent().parent().append(divHtml);
                }
                else {
                    jqPagesContainerDiv.append(divHtml);
                }
                _jqBoundSelectionRect = $("#" + selectionDivID);
            }
            return _jqBoundSelectionRect;
        }

        /**
         * @description 获取选完文本后操作的Div元素jQuery对象,如果不存在，则创建
         * @private
         * @return {Object} 选完文本后操作元素jQuery对象;
         */
        function _getSelectionOperationJQueryObj() {
//            if (!_jqSelectionOperationBarDiv) {
//                var docView = _readerApp.getMainView().getDocView();
//                var firstPageView = docView.getPageView(0);
//                var jqPagesContainerDiv = $("#" + docView.getDocViewContainerID());
//                var selectionOperationDivID = docView.getDocViewContainerID() + "_selection_operation_bar";
//                _copyBtnID = docView.getDocViewContainerID() + "_selection_operation_copy";
//                var divHtml = "<div class='fwr-text-selection-operation-bar fwr-hidden' id='" + selectionOperationDivID + "' >"
//                    + "<ul>"
//                    + "<li >"
//                    + "<a id='" + _copyBtnID + "' href='javascript:void(0);'>" + "Copy" + "</a>"
//                    + "</li>"
//                    + "</ul>"
//                    + "<div class='fwr-text-selection-operation-bar-right'/>"
//                    + "</div>";
//                if (firstPageView != null && firstPageView.isContentCreated()) {
//                    //通过parent去添加，不利于维护，并且效率也不高，后续考虑调整
//                    $("#" + firstPageView.getPageViewContainerID()).parent().parent().append(divHtml);
//                }
//                else {
//                    jqPagesContainerDiv.append(divHtml);
//                }
//                _jqSelectionOperationBarDiv = $("#" + selectionOperationDivID);
//                $("#" + _copyBtnID).on('click', function () {
//                    if (WebPDF.copyText(_readerApp,_self.getSelectedText())) {
//                        _self.hideSelectionOperationDiv();
//                    }
//                });
//            }
        	
        	var menuPlugin = _readerApp.getPluginByName(WebPDF.CONTEXTMENU_PLUGIN_NAME);
            if (_jqSelectionOperationBarDiv == null) {            	
                var pDocView = _readerApp.getMainView().getDocView();
                var pFirstPageView = pDocView.getPageView(0);
                var jqPagesContainerDiv = $("#" + pDocView.getDocViewContainerID());
                var selectionOperationDivID = pDocView.getDocViewContainerID() + "_selection_operation_bar";
                var copyBtnID = pDocView.getDocViewContainerID() + "_selection_operation_copy";
                var divHtml = "<ul class='fwrContextMenu fwr_cm_default' style='display: none;' id='" + selectionOperationDivID + "'>";
                var textSelectionBar = WebPDF.Config.TextSelectionOperationBar;
                for (var i = 0; i < textSelectionBar.length; i++) {
                    divHtml += menuPlugin.createMenuItemHtml(textSelectionBar[i]);
                }
                divHtml += "</ul>";
                $("#" + _readerApp.getMainView().getMainFrameID()).after(divHtml);
                _jqSelectionOperationBarDiv = $("#" + selectionOperationDivID);
            }
            $(".fwr-text-highlight").each(function () {
                $(this).fwrContextMenu(_jqSelectionOperationBarDiv.attr("id"), {
                    onSelect: function (e, context) { return menuPlugin.onSelect($(this)); },
                    onShow: function (e, context) { return menuPlugin.onShow($(this)); }
                },document.getElementById(_readerApp.getMainView().getMainFrameID()));
            });
            return _jqSelectionOperationBarDiv;
        }

        /**
         * Get text page by index.
         * @param pageIndex page index
         * @returns {WebPDF.TextSelect.TextPage | null}
         * @ignore
         */
        this.getTextPage = function (pageIndex) {
            var page = _textPageList[pageIndex];
            if (!page) {
                var textPage = _readerApp.getTextManager().getTextPage(pageIndex, true);
                if (textPage != null) {
                    page = new TextPage();
                    page.textPage = textPage;
                    page.pageSelectInfo.setTextPage(textPage);
                    _textPageList[pageIndex] = page;
                }
            }
            return page;
        };

        /**
         * Hide selection operation div.
         */
        this.hideSelectionOperationDiv = function() {
            var jqSelectionOperationBarDiv = _getSelectionOperationJQueryObj();
            jqSelectionOperationBarDiv.addClass("fwr-hidden");
        };

        /**
         * @description 设置鼠标样式
         * @private
         * @param {WebPDF.TextSelect.CURSOR_STATE} cursorState 设置鼠标样式值
         */
        function _setCursor(cursorState) {
            _cursorState = cursorState;
            var docView = _readerApp.getMainView().getDocView();
            var jqDocViewContainer = $("#" + docView.getDocViewContainerID());
            switch (cursorState) {
                case CURSOR_STATE.CURSOR_ARROW:
                {
                    jqDocViewContainer.removeClass(_curCursorStyleName).css({ 'cursor': 'default' });
                }
                    break;
                case CURSOR_STATE.CURSOR_SELECT:
                case CURSOR_STATE.CURSOR_NORMAL:
                {
                    jqDocViewContainer.removeClass(_curCursorStyleName).css({ 'cursor': 'text' });
                }
                    break;
                case CURSOR_STATE.CURSOR_BOUND:
                {
                    jqDocViewContainer.addClass('fwr-bound-cursor');
                    _curCursorStyleName = "fwr-bound-cursor";
                }
                    break;
                default:
                    break;
            }
        }


        /**
         * @description 判断一个点是否在文本选择区域内
         * @private
         * @param {WebPDF.PDFPoint} pdfPoint 一个点（PDF坐标系）
         * @param {Array.<WebPDF.PDFRect>} rectArray 区域数组
         * @return {Boolean} 在该区域内返回true,否则返回false;
         */
        function _isPtInSelectedRect(pdfPoint, rectArray) {
            for (var i = 0; i < rectArray.length; i++) {
                var rect = rectArray[i];
                if (!RectUtils.isEmpty(rect)) {
                    if (RectUtils.ptInRect(rect, pdfPoint.x, pdfPoint.y)) {
                        return true;
                    }
                }
            }
            return false;
        }

        /**
         * @description 计算选中的文本区域
         * @param {WebPDF.PDFData.Text.CaretInfo} startCaretInfo 开始时插入符信息
         * @param {WebPDF.PDFData.Text.CaretInfo} endCaretInfo 结束时插入符信息
         * @param {Number} startPageIndex 开始时所在页面索引（从0开始）
         * @param {Number} endPageIndex 结束时所在页面索引（从0开始）
         * @return {Boolean} 成功返回true,否则返回false;
         * @private
         */
        function _calcSelectedText(startCaretInfo, endCaretInfo, startPageIndex, endPageIndex) {
           if (startPageIndex < 0 || endPageIndex < 0 || startCaretInfo.index == -1 || endCaretInfo.index == -1)
                return false;
            var temp, tempPageIndex , page , i, isPageSwapped = false;
            if (startPageIndex > endPageIndex) {
                temp = startPageIndex;
                startPageIndex = endPageIndex;
                endPageIndex = temp;
                isPageSwapped = true;
            }
            var startCaretInfoCloned = CommonTools.clone(startCaretInfo);
            var endCaretInfoCloned = CommonTools.clone(endCaretInfo);
            if (isPageSwapped || (startPageIndex===endPageIndex && startCaretInfoCloned.index > endCaretInfoCloned.index)) {
                temp = startCaretInfoCloned.index;
                startCaretInfoCloned.index = endCaretInfoCloned.index;
                endCaretInfoCloned.index = temp;

                temp = startCaretInfoCloned.bLeft;
                startCaretInfoCloned.bLeft = endCaretInfoCloned.bLeft;
                endCaretInfoCloned.bLeft = temp;
            }
            var selectArray = CommonTools.clone(_selectedPageArray);
            _selectedPageArray.length = 0;
            var count = selectArray.length;

            //先清除在指定范围外的页面的选择区域
            for (i = 0; i < count; i++) {
                tempPageIndex = selectArray[i];
                if (tempPageIndex > endPageIndex || tempPageIndex < startPageIndex) {
                    page = _self.getTextPage(tempPageIndex);
                    if (!page) {
                        return false;
                    }
                    page.pageSelectInfo.clearSelectRect();
                    _selectedPageArray.push(tempPageIndex); //这个地方的push很奇怪，在指定范围之外的page 还要push?
                }
            }

            // 如果结束页面和开始页面不相等， 则依次选中开始页面从startCaretInfo开始的文本、
            // 在startPageIndex和endPageIndex之间的所有文本、以及结束页面从开始到endCaretInfo的文本
            if (startPageIndex != endPageIndex) {
                var startTextPage = _self.getTextPage(startPageIndex);
                var endTextPage = _self.getTextPage(endPageIndex);
                if (!startTextPage || !endTextPage) {
                    return false;
                }
                startTextPage.pageSelectInfo.addSelectedRectByIndex(startCaretInfoCloned.bLeft ? startCaretInfoCloned.index :
                    startCaretInfoCloned.index + 1,startTextPage.textPage.getSize() - 1);
                _selectedPageArray.push(startPageIndex);

                for (i = startPageIndex + 1; i < endPageIndex; i++) {
                    page = _self.getTextPage(i);
                    if (!page) {
                        return false;
                    }
                    page.pageSelectInfo.selectAll();
                    if (page.pageSelectInfo.isSelected()) {
                        _selectedPageArray.push(i);
                    }
                }
                endTextPage.pageSelectInfo.addSelectedRectByIndex(0, endCaretInfoCloned.bLeft ? endCaretInfoCloned.index - 1 :
                    endCaretInfoCloned.index);
                _selectedPageArray.push(endPageIndex);

                return _selectedPageArray.length > 0;
            }
            else {
                var startIndex = startCaretInfoCloned.index;
                var endIndex = endCaretInfoCloned.index;
                if (startCaretInfoCloned.index != endCaretInfoCloned.index) {
                    if (startCaretInfoCloned.bLeft == false) {
                        startIndex++;
                    }
                    if (endCaretInfoCloned.bLeft == true) {
                        endIndex--;
                    }
                    page = _self.getTextPage(startPageIndex);
                    if (!page) {
                        return false;
                    }
                    page.pageSelectInfo.addSelectedRectByIndex(startIndex, endIndex);
                    _selectedPageArray.push(startPageIndex);
                    return true;
                } else {
                    if (startCaretInfoCloned.bLeft != endCaretInfoCloned.bLeft) {
                        page = _self.getTextPage(startPageIndex);
                        if (!page) {
                            return false;
                        }
                        page.pageSelectInfo.addSelectedRectByIndex(startCaretInfoCloned.index, endCaretInfoCloned.index);
                        _selectedPageArray.push(startPageIndex);
                        return true;
                    } else {
                        if (selectArray.length == 0) {
                            return false;
                        }
                        page = _self.getTextPage(startPageIndex);
                        if (!page) {
                            return false;
                        }
                        page.pageSelectInfo.clearSelectRect();
                        _selectedPageArray.push(startPageIndex);
                        return true;
                    }
                }
            }

            return false;
        }

        /**
         * @description 计算范围选择标记的区域, 目前只支持在单个页面里面框选
         * @param {WebPDF.PDFPoint} startPoint 开始点
         * @param {WebPDF.PDFPoint} endPoint 结束点
         * @param {Number} startPageIndex 开始时所在页面索引（从0开始）
         * @return {Boolean} 成功返回true,否则返回false;
         * @private
         */
        function _calcSelectBoundText(startPoint, endPoint, startPageIndex) {
            if (startPoint.equal(endPoint)) {
                if (_selectedPageArray.length == 0) {
                    return false;
                }
                else {
                    _self.clearSelection();
                    return false;
                }
            }
            var startPDFPoint = new PDFPoint(0, 0);
            var endPDFPoint = new PDFPoint(0, 0);
            startPDFPoint = _curPageView.docPtToPDF(startPoint);
            endPDFPoint = _curPageView.docPtToPDF(endPoint);
            var rect = new PDFRect(startPDFPoint.x, startPDFPoint.y, endPDFPoint.x, endPDFPoint.y);
            var page = _self.getTextPage(startPageIndex);
            if (!page) {
                return false;
            }
            page.pageSelectInfo.addSelectedRect(rect);
            _selectedPageArray.length = 0;
            _selectedPageArray.push(startPageIndex);
            return true;
        }

        /**
         * @description left mouse button down event
         * @param {object} event event
         */
        this.onLButtonDown = function (event) {
            if(event.target.id === _copyBtnID){
                return false;
            }
            _beginSelectionPoint.x = event.pageX;
            _beginSelectionPoint.y = event.pageY;
            var pageView, ptOffsetCurPageView = new PDFPoint(0, 0);
            pageView = CReader_TextSelectTool.getPageViewInfoByMouseEvent(_readerApp, event,
                ptOffsetCurPageView, true, false);
            //如果不是点击在页面上面，则清除选中文本
            if (!pageView) {
                var docView = _readerApp.getMainView().getDocView();
                var tmpPt = new PDFPoint(event.pageX, event.pageY);
                var docViewRect = docView.getDocViewClientRect();
                if (RectUtils.ptInRect(docViewRect, tmpPt)) {
                    this.clearSelection();
                }
                return;
            }
            //如果点击在页面上，先屏蔽浏览器默认行为
            CommonTools.preventDefaults(event, true);
            _isLButtonDown = true;
            var pdfPoint = new PDFPoint(0, 0);
            pdfPoint = pageView.docPtToPDF(ptOffsetCurPageView);
            var rectArray = [];
            var pageIndex = pageView.getPageIndex();
            var textPage = _self.getTextPage(pageIndex);
            if (!textPage) {
                return;
            }
            textPage.pageSelectInfo.getSelectPDFRect(rectArray);
            //如果点在已经被选择的文本区域里
            if (_isPtInSelectedRect(pdfPoint, rectArray)) {
                _selectMode = SELECT_STATE.SELECT_MODE_IN_SELECTED;
                _setCursor(CURSOR_STATE.CURSOR_ARROW);
                return;
            }

            var charIndex = textPage.textPage.getCharIndexByPoint(pdfPoint);
            var caretInfo;
            if (charIndex != -1) {
                caretInfo = new CaretInfo();
                textPage.textPage.getCaretAtPoint(pdfPoint, caretInfo);
                if (caretInfo.index == -1) {
                    //旋转字体有可能返回-1值;
                    charIndex = -1;
                }
            }

            //得到ALT键的状态; 高位:1是down,0是up ;决定了是否必须采用BOUND方式
            var isAltKeyDown = event.altKey;
            if (_isBoundSelect) {
                //清空文本选择;
                this.clearSelection();
                _isBoundSelect = false;
                _curCaretInfo.index = -1;
            }
            //在文本上且没有按住Alt键（如果按住Alt键则使用选框选择）
            if (charIndex >= 0 && !isAltKeyDown) {
                var isShiftKeyDown = event.shiftKey;
                _selectMode = SELECT_STATE.SELECT_MODE_SELECTING;
                _setCursor(CURSOR_STATE.CURSOR_SELECT);
                if (_startCaretInfo.index < 0) {
                    if (_curPageView != pageView) {
                        _curPageView = pageView;
                    }
                    textPage.textPage.getCaretAtPoint(pdfPoint, _curCaretInfo);
                    _startCaretInfo = CommonTools.clone(_curCaretInfo);
                    _startPageIndex = _curPageView.getPageIndex();
                    if (_curCaretInfo.index < 0) {
                        return;
                    }
                }
                else if (!isShiftKeyDown) //当没有按住Shift键
                {
                    if (_startCaretInfo.index < 0) {
                        return;
                    }
                    this.clearSelection();
                    caretInfo = new CaretInfo();
                    textPage.textPage.getCaretAtPoint(pdfPoint, caretInfo);
                    if (_curPageView != pageView) {
                        _curPageView = pageView;
                    }
                    _curCaretInfo = caretInfo;
                    _startCaretInfo = CommonTools.clone(_curCaretInfo);
                    _startPageIndex = _curPageView.getPageIndex();
                }
                else if (isShiftKeyDown) //按住Shift键;
                {
                    if (_startCaretInfo.index == -1) {
                        return;
                    }
                    caretInfo = new CaretInfo();
                    textPage.textPage.getCaretAtPoint(pdfPoint, caretInfo);
                    if (_curPageView != pageView) {
                        _curPageView = pageView;
                    }
                    _curCaretInfo = caretInfo;
                    _calcSelectedText(_startCaretInfo, _curCaretInfo, _startPageIndex, _curPageView.getPageIndex());
                }
            }
            else //在文本上且按alt键或者不在文本上
            {
                //清空文本选择;
                this.clearSelection();
                _curPageView = pageView;

                if (charIndex >= 0) {
                    textPage.textPage.getCaretAtPoint(pdfPoint, _startCaretInfo);
                    _startPageIndex = _curPageView.getPageIndex();
                }
                _curCaretInfo.index = -1;
                _startWindowPoint = _endWindowPoint = ptOffsetCurPageView;
                _selectMode = SELECT_STATE.SELECT_MODE_BOUND;
                _setCursor(CURSOR_STATE.CURSOR_BOUND);
            }
        };

        /**
         * @description 处理选择文本鼠标左键弹起事件
         * @param {object} event 鼠标事件对象
         * @param {Function | null} callbackForSelected 选择文本后的回调函数
         */
        this.onLButtonUp = function (event, callbackForSelected) {
            _isLButtonDown = false;
            _hideSelectBoundBox();
            var pageView , ptOffsetCurPageView = new PDFPoint(0, 0);
            pageView = CReader_TextSelectTool.getPageViewInfoByMouseEvent(_readerApp, event,
                ptOffsetCurPageView, true, false);
            if (!pageView) {
                return;
            }
            var pdfPoint = new PDFPoint(0, 0);
            pdfPoint = pageView.docPtToPDF(ptOffsetCurPageView);
            var pageIndex = pageView.getPageIndex();
            var textPage = _self.getTextPage(pageIndex);
            if (!textPage) {
                return;
            }
            var rectArray = [];
            textPage.pageSelectInfo.getSelectPDFRect(rectArray);
            var isPtInSelectedRect = _isPtInSelectedRect(pdfPoint, rectArray);
            if (_selectMode == SELECT_STATE.SELECT_MODE_IN_SELECTED) {
                //如果点在已经被选择的文本区域里;
                if (isPtInSelectedRect) {
                    this.clearSelection();
                }
            }
            if (isPtInSelectedRect && _cursorState != CURSOR_STATE.CURSOR_ARROW) {
                _setCursor(CURSOR_STATE.CURSOR_ARROW);
            }
            else if (!isPtInSelectedRect && _cursorState != CURSOR_STATE.CURSOR_SELECT) {
                _setCursor(CURSOR_STATE.CURSOR_SELECT);
            }
            _selectMode = SELECT_STATE.SELECT_MODE_NORMAL;
            _self.highLightAllSelectedText();
            if (this.isSelected()) {
                if ($.isFunction(callbackForSelected)) {
                    callbackForSelected(event);
                } else {
                	_getSelectionOperationJQueryObj();
                    _copyTextToFrame();
//                    if (event.pageY >= _beginSelectionPoint.y) {
//                        if (rectArray.length > 0)
//                            _showOperationBar(pageView, rectArray, true);
//                        else {
//                            var previousPageView = _getAdjacentSelectPageRects(rectArray, pageIndex, true);
//                            if (previousPageView) {
//                                _showOperationBar(previousPageView, rectArray, true);
//                            }
//                        }
//
//                    }
//                    else {
//                        if (rectArray.length > 0)
//                            _showOperationBar(pageView, rectArray, false);
//                        else {
//                            var nextPageView = _getAdjacentSelectPageRects(rectArray, pageIndex, false);
//                            if (nextPageView) {
//                                _showOperationBar(nextPageView, rectArray, false);
//                            }
//                        }
//                    }
                }
            }
            _isLButtonDown = false;
        };

        /**
         * @description 获取当前页上一页选择文本的区域
         * @private
         * @param {Array.<WebPDF.PDFRect>} rectArray 选择文本区域数组
         * @param {Number} pageIndex 当前页面索引（从0开始）
         * @param {Boolean} isPrevious 为true代表向上搜索，false代表向下搜索
         * @return {WebPDF.PDFView.PDFPageView} 返回上一页的页面视图对象;
         */
        function _getAdjacentSelectPageRects(rectArray, pageIndex, isPrevious) {
            var count = _selectedPageArray.length;
            var pageView = null;
            for (var i = count - 1; i >= 0; i--) {
                var index = _selectedPageArray[i];
                if ((isPrevious && index >= pageIndex) || (!isPrevious && index <= pageIndex)) {
                    continue;
                }
                var textPage = _self.getTextPage(index);
                if (!textPage) {
                    continue;
                }
                if (textPage.pageSelectInfo.getSelectPDFRect(rectArray) != 0) {
                    pageView = _readerApp.getMainView().getDocView().getPageView(index);
                    return pageView;
                }
            }
            return pageView;
        }


        /**
         * @description 获取页面相应标记文本选中区域Div的ID
         * @private
         * @param {WebPDF.PDFView.PDFPageView} pageView 页面视图对象
         * @return {String} 返回标记文本选中区域Div的ID;
         */
        function _getTextHighlightDivID(pageView) {
            return pageView.getPageViewContainerID() + "_TextHighLights";
        }

        /**
         * @description 显示文本选择后的操作Div
         * @private
         * @param {WebPDF.PDFView.PDFPageView} pageView 页面视图对象
         * @param {Array.<WebPDF.PDFRect>} selectedPDFRects 页面选择区域数组
         * @param {Boolean} isSelectDown 是否是向下选择文本
         */
        function _showOperationBar(pageView, selectedPDFRects, isSelectDown) {
            var jqSelectionOperationDiv = _getSelectionOperationJQueryObj();
            if (!jqSelectionOperationDiv.hasClass("fwr-hidden")) {
                return;
            }
            var curNearRect = _getSelectedTextCornerRect(selectedPDFRects, !isSelectDown);
            if (!curNearRect) {
                return;
            }
            curNearRect = pageView.pdfToDoc(curNearRect, true);
            var pageOffsetHtmlDoc = $("#" + pageView.getPageViewContainerID()).offset();
            var leftPos = 0, topPos = 0;
            if (isSelectDown) {
                leftPos = curNearRect.right + pageOffsetHtmlDoc.left;
                topPos = curNearRect.bottom + pageOffsetHtmlDoc.top;
            }
            else {
                leftPos = curNearRect.left + pageOffsetHtmlDoc.left;
                topPos = curNearRect.top + pageOffsetHtmlDoc.top;
            }

            //jqSelectionOperationDiv.removeClass("fwr-hidden");
            var docView = _readerApp.getMainView().getDocView();
            var docViewRect = docView.getDocViewClientRect();
            var scrollApi = docView.getScrollApi();
            var scrollPosX = 0, scrollPosY = 0;
            if (scrollApi) {
                scrollPosX = scrollApi.getContentPositionX();
                scrollPosY = scrollApi.getContentPositionY();
            }
            leftPos = leftPos - docViewRect.left + scrollPosX;
            topPos = topPos - docViewRect.bottom + scrollPosY;
            if (!isSelectDown) {
                var divWidth = jqSelectionOperationDiv.width();
                var divHeight = jqSelectionOperationDiv.height();
                leftPos = leftPos - divWidth;
                topPos = topPos - divHeight;
            }
            jqSelectionOperationDiv.css({ left: leftPos, top: topPos });
        }

        /**
         * @description Get selected text.
         * @return {String} selected text.
         */
        this.getSelectedText = function () {
            var allSelectedText = "";
            var count = _selectedPageArray.length;
            for (var i = 0; i < count; i++) {
                var index = _selectedPageArray[i];
                var textPage = _self.getTextPage(index);
                if (textPage && textPage.textPage) {
                    allSelectedText += textPage.pageSelectInfo.getSelectedText();
                    if (i != count - 1) {
                        allSelectedText += WebPDF.TEXT_RETURN_LINEFEED;
                    }
                }
            }
            return allSelectedText;
        };

        /**
         * @description Get rect information of selected text.
         * @return {Array.<WebPDF.TextSelect.TextSelectedInfo>} rect information of selected text.
         */
        this.getSelectedTextRectInfo = function () {
            var selectedPageRectArray = [];
            for (var i = 0; i < _selectedPageArray.length; i++) {
                var index = _selectedPageArray[i];
                var textPage = _self.getTextPage(index);
                if (!textPage) {
                    continue;
                }
                if (textPage.pageSelectInfo.isSelected()) {
                    var rectArray = [];
                    textPage.pageSelectInfo.getSelectPDFRect(rectArray);
                    var textSelectInfo = new WebPDF.TextSelect.TextSelectedInfo();
                    textSelectInfo.pageIndex = index;
                    textSelectInfo.selectedRectArray = rectArray;
                    selectedPageRectArray.push(textSelectInfo);
                }
            }
            return selectedPageRectArray;
        };

        /**
         * @description Clear all selected text.
         */
        this.clearSelection = function () {
            _clearAllHighlightText();
            _self.hideSelectionOperationDiv();
        };

        /**
         * @description 清除某页面视图的选择文本
         * @param {WebPDF.PDFView.PDFPageView} pageView 页面视图对象
         * @private
         */
        function _clearHighLightText(pageView) {
            if (pageView != null && pageView.isContentCreated()) {
                var highLightDivID = _getTextHighlightDivID(pageView);
                var jqHighlightDiv = $("#" + highLightDivID);
                if (jqHighlightDiv != null) {
                    jqHighlightDiv.empty();
                }
            }
        }

        /**
         * @description 隐藏所有选择文本区域标记Div
         * @private
         */
        function _clearAllHighlightText() {
            var docView = _readerApp.getMainView().getDocView();
            for (var i = 0; i < _selectedPageArray.length; i++) {
                var index = _selectedPageArray[i];
                var textPage = _self.getTextPage(index);
                if (textPage) {
                    textPage.pageSelectInfo.clearSelectRect();
                    _clearHighLightText(docView.getPageView(index));
                }
            }
        }

        /**
         * @description 获取选中的文本区域的左上角矩形区域或者右下角矩形区域
         * @param {Array.<WebPDF.PDFRect>} pdfRects 区域数组
         * @param {Boolean} isLeftTop 该值为true则返回处于左上角的文本选中区域，否则返回右下角文本选中区域
         * @private
         * @return {WebPDF.PDFRect} 选中的文本区域
         */
        function _getSelectedTextCornerRect(pdfRects, isLeftTop) {
            var targetRect = null;
            if (pdfRects == null || pdfRects.length <= 0)
                return null;
            targetRect = pdfRects[0];
            RectUtils.normalize(targetRect);
            for (var i = 1; i < pdfRects.length; i++) {
                var rect = pdfRects[i];
                RectUtils.normalize(rect);
                if (isLeftTop) {
                    if (rect.top > targetRect.top) { //pdf坐标系从下到上，所以取最大的top，转到document里，就是最小的top
                        targetRect = rect;
                    }
                    else if (rect.top == targetRect.top) {
                        if (rect.left < targetRect.left) {
                            targetRect = rect;
                        }
                    }
                }
                else {
                    if (rect.bottom < targetRect.bottom) { //pdf坐标系从下到上，所以取最小的bottom，转到document里，就是最大的bottom
                        targetRect = rect;
                    }
                    else if (rect.bottom == targetRect.bottom) {
                        if (rect.right > targetRect.right) {
                            targetRect = rect;
                        }
                    }
                }

            }
            return targetRect;
        }

        /**
         * @description 高亮指定页面的选中文本
         * @param {WebPDF.PDFView.PDFPageView} pageView 页面视图对象
         * @param {Array.<WebPDF.PDFRect>} highlightRects 需要高亮的矩形区域列表
         * @private
         */
        function _highLightSelectedText(pageView, highlightRects) {
            if (!pageView || !pageView.isContentCreated() || !highlightRects)
                return false;
            var highLightDivID = _getTextHighlightDivID(pageView);
            var pageIndex = pageView.getPageIndex();
            if (!_highlightDivCreatedPageList[pageIndex]) {
                var highLightDivHtml = "<div id='" + highLightDivID + "' style='z-index:7;'></div>";
                var jqPageView = $("#" + pageView.getPageViewContainerID());
                jqPageView.append(highLightDivHtml);
                _highlightDivCreatedPageList[pageIndex] = true;
            }
            var jqHighlightDiv = $("#" + highLightDivID);
            jqHighlightDiv.empty(); //清空原有的div内容;

            var highLightDivContent = "";
            for (var i = 0; i < highlightRects.length; i++) {
                var rect = highlightRects[i];
                RectUtils.normalize(rect);
                rect = pageView.pdfToDoc(rect, true);
                highLightDivContent += "<div id='" + "text_highlight_" + pageIndex + "_" + i + "'class='fwr-text-highlight' style='left:" +
                    rect.left + "px;top:" + rect.top + "px;width:" + RectUtils.width(rect) + "px;height:" + RectUtils.height(rect) + "px;z-index:1000' ></div>";
            }
            jqHighlightDiv.append(highLightDivContent);
        }        
        

        /**
         * @description Highlight all text.
         */
        this.highLightAllSelectedText = function() {
            var count = _selectedPageArray.length;
            for (var i = 0; i < count; i++) {
                var index = _selectedPageArray[i];
                var textPage = _self.getTextPage(index);
                if (!textPage) {
                    continue;
                }
                var rectArray = [];
                textPage.pageSelectInfo.getSelectPDFRect(rectArray);
                _highLightSelectedText(_readerApp.getMainView().getDocView().getPageView(index), rectArray);
            }
        };

        /**
         * @description Handler for left mouse double click event.
         * @param {object} event event object.
         * @param {Function | null} callbackForSelected callback function after text selected,
         * if this parameter is null, internal callback will be called.
         */
        this.onLButtonDblClk = function (event, callbackForSelected) {
            if (!event || _selectMode === SELECT_STATE.SELECT_MODE_BOUND) {
                return;
            }
            var pageView = null, ptOffsetCurPageView = new PDFPoint(0, 0);
            pageView = CReader_TextSelectTool.getPageViewInfoByMouseEvent(_readerApp, event, ptOffsetCurPageView
                , true, false);
            if (!pageView) {
                return;
            }
            _curPageView = pageView;
            var pageIndex = pageView.getPageIndex();
            var textPage = _self.getTextPage(pageIndex);
            if (!textPage) {
                return;
            }
            var pdfPoint = new PDFPoint(0, 0);
            pdfPoint = pageView.docPtToPDF(ptOffsetCurPageView);
            var charIndex = textPage.textPage.getCharIndexByPoint(pdfPoint);
            if (charIndex != -1) {
                var caretInfo = new CaretInfo();
                textPage.textPage.getCaretAtPoint(pdfPoint, caretInfo);
                if (caretInfo.index == -1) {
                    //旋转字体有可能返回-1值;
                    charIndex = -1;
                }
            }
            if (charIndex != -1) {
                var wordRangeInfo = new TextBoundInfo(-1, -1);
                if (textPage.textPage.getWordRange(charIndex, wordRangeInfo)) {
                    if (wordRangeInfo.startIndex === -1 || wordRangeInfo.endIndex === -1) {
                        return;
                    }
                    textPage.pageSelectInfo.addSelectedRectByIndex(wordRangeInfo.startIndex, wordRangeInfo.endIndex);
                    _selectedPageArray.length = 0;
                    _selectedPageArray.push(pageIndex);
                    if ($.isFunction(callbackForSelected)) {
                        callbackForSelected(event);
                    } else {
                        var rectArray = [];
                        textPage.pageSelectInfo.getSelectPDFRect(rectArray);
                        _copyTextToFrame();
                        //_showOperationBar(pageView, rectArray, true);
                        _self.highLightAllSelectedText();
                        _getSelectionOperationJQueryObj();
                    }
                }
            }
        };
        /**
         * @description Handler for left mouse move event.
         * @param {object} event mouse event object
         */
        this.onMouseMove = function (event) {
            if (!_isLButtonDown) {
                return;
            }
            var pageView, ptOffsetCurPageView = new PDFPoint(0, 0);
            pageView = CReader_TextSelectTool.getPageViewInfoByMouseEvent(_readerApp, event,
                ptOffsetCurPageView, true, true);
            if (pageView == null) {
                return;
            }
            //如果鼠标左键已经按下，则屏蔽浏览器默认行为
            if (_isLButtonDown) {
                WebPDF.Common.preventDefaults(event, true);
            }
            var pdfPoint = new PDFPoint(0, 0);
            pdfPoint = pageView.docPtToPDF(ptOffsetCurPageView);
            var pageIndex = pageView.getPageIndex();
            var textPage = _self.getTextPage(pageIndex);
            if (!textPage) {
                return;
            }

            var rectArray = [];
            textPage.pageSelectInfo.getSelectPDFRect(rectArray);
            var isPtInRect = _isPtInSelectedRect(pdfPoint, rectArray); //如果点在已经被选择的文本区域里;
            var charIndex = textPage.textPage.getCharIndexByPoint(pdfPoint);
            var caretInfo;
            if (charIndex != -1) {
                caretInfo = new CaretInfo();
                textPage.textPage.getCaretAtPoint(pdfPoint, caretInfo);
                if (caretInfo.index == -1) {
                    //旋转字体有可能返回-1值;
                    charIndex = -1;
                }
            }
            var isAltKeyDown = event.altKey;
            if (_selectMode == SELECT_STATE.SELECT_MODE_NORMAL) {
                if (isPtInRect) {
                    _setCursor(CURSOR_STATE.CURSOR_ARROW);
                }
                else if (charIndex >= 0 && !isAltKeyDown) {
                    _setCursor(CURSOR_STATE.CURSOR_SELECT);
                }
                else if (charIndex >= 0 && isAltKeyDown) {
                    _setCursor(CURSOR_STATE.CURSOR_BOUND);
                }
                else {
                    _setCursor(CURSOR_STATE.CURSOR_NORMAL);
                }
                return;
            }

            if (_selectMode == SELECT_STATE.SELECT_MODE_BOUND) {
                if (_cursorState != CURSOR_STATE.CURSOR_BOUND) {
                    return;
                }
                _isBoundSelect = true;
                _endWindowPoint = ptOffsetCurPageView;
                var curPageIndex = _curPageView.getPageIndex();
                if (pageIndex != curPageIndex) {
                    if (pageIndex < curPageIndex) {
                        _endWindowPoint.y = 0;
                    }
                    else {
                        _endWindowPoint.y = _curPageView.getPageViewHeight();
                    }
                }

                if (_calcSelectBoundText(_startWindowPoint, _endWindowPoint, curPageIndex, pageIndex)) {
                    _self.highLightAllSelectedText();
                    textPage.pageSelectInfo.getFirstCaretInfo(_curCaretInfo);
                }
                _endSelectionPoint.x = event.pageX;
                _endSelectionPoint.y = event.pageY;
                _drawSelectBoundBox();
            }
            else if (_selectMode == SELECT_STATE.SELECT_MODE_SELECTING) {
                if (_cursorState != CURSOR_STATE.CURSOR_SELECT)
                    return;
                if (!_curPageView || _curCaretInfo.index < 0)
                    return;
                if (_curPageView != pageView) {
                    _curPageView = pageView;
                }
                caretInfo = new CaretInfo();
                if (textPage.textPage.getCaretAtPoint(pdfPoint, caretInfo)) {
                    _curCaretInfo = caretInfo;
                    if (_calcSelectedText(_startCaretInfo, _curCaretInfo, _startPageIndex, _curPageView.getPageIndex())) {
                        _self.highLightAllSelectedText();
                    }
                }
                _scrollPage(new PDFPoint(event.pageX, event.pageY));
            }
        };

        /**
         * @description 显示范围选择框
         * @private
         */
        function _drawSelectBoundBox() {
            var jqBoundSelection = _getBoundSelectionRectJQueryObj();
            var x = Math.abs(_endSelectionPoint.x - _beginSelectionPoint.x);
            var y = Math.abs(_endSelectionPoint.y - _beginSelectionPoint.y);
            var l = null, t = null;
            // A part
            if (_endSelectionPoint.x < _beginSelectionPoint.x && _endSelectionPoint.y < _beginSelectionPoint.y) {
                l = _endSelectionPoint.x;
                t = _endSelectionPoint.y;
            }

            // B part
            if (_endSelectionPoint.x > _beginSelectionPoint.x && _endSelectionPoint.y < _beginSelectionPoint.y) {
                l = _beginSelectionPoint.x;
                t = _endSelectionPoint.y;
            }

            // C part
            if (_endSelectionPoint.x < _beginSelectionPoint.x && _endSelectionPoint.y > _beginSelectionPoint.y) {
                l = _endSelectionPoint.x;
                t = _beginSelectionPoint.y;
            }

            // D part
            if (_endSelectionPoint.x > _beginSelectionPoint.x && _endSelectionPoint.y > _beginSelectionPoint.y) {
                l = _beginSelectionPoint.x;
                t = _beginSelectionPoint.y;
            }

            var docView = _readerApp.getMainView().getDocView();
            var docViewRect = docView.getDocViewClientRect();
            var scrollApi = docView.getScrollApi();
            var scrollPosX = 0, scrollPosY = 0;
            if (scrollApi) {
                scrollPosX = scrollApi.getContentPositionX();
                scrollPosY = scrollApi.getContentPositionY();
            }
            l = l - docViewRect.left + scrollPosX;
            t = t - docViewRect.bottom + scrollPosY;
            jqBoundSelection.css({ left: l, top: t, width: x, height: y }).removeClass("fwr-hidden");
        }

        /**
         * @description 隐藏范围选择区域标记Div
         * @private
         */
        function _hideSelectBoundBox() {
            var jqBoundSelection = _getBoundSelectionRectJQueryObj();
            jqBoundSelection.addClass("fwr-hidden");
        }

        /**
         * @description 依据一个点，将滚动条滚动到该点所在页面显示范围
         * @private
         * @param {WebPDF.PDFPoint} point 一个点对象
         * @return {Boolean} 成功返回true,否则返回false;
         */
        function _scrollPage(point) {
            if (!_curPageView) {
                return false;
            }
            var docView = _readerApp.getMainView().getDocView();
            var jqDocView = $("#" + docView.getDocViewContainerID());
            var docHeight = jqDocView.height(), docWidth = jqDocView.width() - 16,
                docOffset = jqDocView.offset();
            var scrollApi = docView.getScrollApi();
            if (scrollApi) {
                docWidth -= scrollApi.getScrollBarWidth();
                docHeight -= scrollApi.getScrollBarHeight();
            }
            var docViewRect = new PDFRect(docOffset.left, docOffset.top, docOffset.left + docWidth, docOffset.top + docHeight);
            RectUtils.normalize(docViewRect);
            if (RectUtils.ptInRect(docViewRect, point)) {
                return false;
            }
            var cx = 0, cy = 0, offset = 20;
            if (point.x < docViewRect.left)
                cx = -offset;
            if (point.y < docViewRect.bottom)
                cy = -offset;
            if (point.x > docViewRect.right)
                cx = offset;
            if (point.y > docViewRect.top)
                cy = offset;
            docView.movePage(cx, cy);
            return true;
        }

        /**
         * @description Check whether any text has been selected.
         * @return {Boolean} return true if some text has been selected, otherwise return false.
         */
        this.isSelected = function () {
            var count = _selectedPageArray.length;
            for (var i = 0; i < count; i++) {
                var textPage = _self.getTextPage(_selectedPageArray[i]);
                if (!textPage) {
                    continue;
                }
                var rectArray = [];
                if (textPage.pageSelectInfo.getSelectPDFRect(rectArray) > 0) {
                    return true;
                }
            }
            return false;
        };

        /**
         * Get range of selected pages.
         * @returns {{start: number, end: number}} range of selected pages.
         */
        this.getSelectedPageRange = function () {
            var startPage = _readerApp.getMainView().getDocView().getPageCount();
            var endPage = -1;
            for (var i = 0; i < _selectedPageArray.length; i++) {
                if (_selectedPageArray[i] < startPage) {
                    startPage = _selectedPageArray[i];
                }
                if (_selectedPageArray[i] > endPage) {
                    endPage = _selectedPageArray[i];
                }
            }
            return {start: startPage, end: endPage};
        };
    };


    CReader_TextSelectTool.ToolInstances = {};
    /**
     * @description Get an instance of text selection tool, if the instance does not exist, it will be created.
     * @static
     * @param {WebPDF.ReaderApp} readerApp app instance
     * @return {WebPDF.TextSelect.CReader_TextSelectTool} an instance of text selection tool
     */
    CReader_TextSelectTool.getTextSelectTool = function (readerApp) {
        var instance = CReader_TextSelectTool.ToolInstances[readerApp.getAppId()];
        if (!instance) {
            instance = new CReader_TextSelectTool(readerApp);
            CReader_TextSelectTool.ToolInstances[readerApp.getAppId()] = instance;
        }
        return instance;
    };

    /**
     * @description 验证当前鼠标点击所在的是否在该页面
     * @param {WebPDF.PDFView.PDFPageView} pageView 页面视图对象
     * @param {object} event 鼠标事件
     * @param {WebPDF.PDFPoint} ptOffsetCurPageView 用来返回当前鼠标位置在当前页面中的坐标
     * @return {Boolean} 在该页面区域内则返回true,否则返回false;
     * @static
     * @ignore
     */
    CReader_TextSelectTool.getPtOffsetPageView = function (pageView, event, ptOffsetCurPageView) {
        if (!pageView)
            return false;
        var pageOffsetHtmlDoc = $("#" + pageView.getPageViewContainerID()).offset();
        var pageViewHeight = pageView.getPageViewHeight() + 14;
        var pageViewRect = new PDFRect(0, pageOffsetHtmlDoc.top,
            pageView.getDocView().getDocViewWidth(), pageOffsetHtmlDoc.top + pageViewHeight);
        RectUtils.normalize(pageViewRect);
        ptOffsetCurPageView.x = event.pageX - pageOffsetHtmlDoc.left;
        ptOffsetCurPageView.y = event.pageY - pageOffsetHtmlDoc.top;
        return RectUtils.ptInRect(pageViewRect, event.pageX, event.pageY);
    };

    /**
     * @description 获取当前鼠标点击所在的页面,并且返回鼠标位置相对于文档视图的偏移量
     * @param {WebPDF.ReaderApp} readerApp 应用类实例
     * @param {object} event 鼠标事件
     * @param {WebPDF.PDFPoint} ptOffsetCurPageView 用来返回当前鼠标位置在当前页面中的坐标
     * @param {Boolean} bGetCurPageView 为true:当鼠标点没在显示页面中时，会再查找当前页面；否则不会
     * @param {Boolean} bEnableOutOfDocView 是否允许在文档视图区域之外
     * @return {WebPDF.PDFView.PDFPageView} 返回当前鼠标点击所在的页面对象;
     * @static
     * @ignore
     */
    CReader_TextSelectTool.getPageViewInfoByMouseEvent = function (readerApp, event, ptOffsetCurPageView, bGetCurPageView, bEnableOutOfDocView) {
        var docView = readerApp.getMainView().getDocView();
        var visiblePageRange = docView.getVisiblePageRange();
        var curClickPageView = null;
        var tmpPt = new PDFPoint(event.pageX, event.pageY);
        var docViewRect = docView.getDocViewClientRect();
        if (!bEnableOutOfDocView && !RectUtils.ptInRect(docViewRect, tmpPt)) {
            return curClickPageView;
        }
        var pageView;
        for (var i = visiblePageRange.begin; i <= visiblePageRange.end; i++) {
            pageView = docView.getPageView(i);
            if (pageView == null)
                continue;
            if (CReader_TextSelectTool.getPtOffsetPageView(pageView, event, ptOffsetCurPageView)) {
                curClickPageView = pageView;
                break;
            }
        }
        if (!curClickPageView && bGetCurPageView) {
            pageView = docView.getPageView(docView.getCurPageIndex());
            CReader_TextSelectTool.getPtOffsetPageView(pageView, event, ptOffsetCurPageView);
            curClickPageView = pageView;
        }
        return curClickPageView;
    };

    WebPDF.TextSelect.CReader_TextSelectTool = CReader_TextSelectTool;

	//静态函数
    WebPDF.TextSelect.CReader_TextSelectTool.Instance = null;
    /**
    * @description （静态方法）获取FXREADER.TextSelect.CReader_TextSelectTool的一个实例，如果之前已创建就返回之前的，否则新创建
    * @static
    * @param {FXREADER.CReaderApp} pReaderApp App实例对象
    * @param {Boolean} bUseReaderDocTextpage 是否使用文本页面
    * @return {FXREADER.TextSelect.CReader_TextSelectTool} 返回文本选择处理对象;
    */
    WebPDF.TextSelect.CReader_TextSelectTool.CreateTextSelectTool = function (pReaderApp, bUseReaderDocTextpage) {
        if (WebPDF.TextSelect.CReader_TextSelectTool.Instance == null) {
        	WebPDF.TextSelect.CReader_TextSelectTool.Instance = new WebPDF.TextSelect.CReader_TextSelectTool(pReaderApp, bUseReaderDocTextpage);
        }
        return WebPDF.TextSelect.CReader_TextSelectTool.Instance;
    }
	
    return WebPDF.TextSelect.CReader_TextSelectTool;
});